package com.zifan.yygh.controller.api;

import com.zifan.common.result.Result;
import com.zifan.yygh.helper.HttpRequestHelper;
import com.zifan.yygh.model.hosp.Department;
import com.zifan.yygh.model.hosp.Hospital;
import com.zifan.yygh.model.hosp.Schedule;
import com.zifan.yygh.service.DepartmentService;
import com.zifan.yygh.service.HospitalService;
import com.zifan.yygh.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 说明：平台对外开发的接口都写在该Controller类
 * @author Zifan
 * @date 2021年09月26日15:00
 */
@SuppressWarnings("all")
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        /**
         * 这里解释下为什么用request的方式来接收参数，而不直接用对象来接收。
         * 原因是，调用这个接口的其他系统使用post发的请求，请求参数是表单的形式提交的。而不是放在请求体里面。
         */
        Map<String, Object> paramsMap = getStringObjectMap(request);

        hospitalService.saveHospital(paramsMap);
        return Result.ok();
    }


    //查询医院接口
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, Object> paramsMap = getStringObjectMap(request);

        Hospital hospital = hospitalService.showHospital(paramsMap);
        return Result.ok(hospital);
    }

    //上传科室接口
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        departmentService.saveDepartment(paramMap);
        return Result.ok();
    }

    //分页获取科室信息
    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        Page<Department> page =  departmentService.getDepartmentList(paramMap);
        return Result.ok(page);
    }

    //根据id删除科室信息
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        departmentService.removeDepartment(paramMap);
        return Result.ok();
    }

    //上传排班信息
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }

    //条件，分页获取排班信息
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        Page<Schedule> schedules = scheduleService.getScheduleList(paramMap);
        return Result.ok(schedules);
    }

    //删除排班信息
    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = getStringObjectMap(request);
        scheduleService.removeSchedule(paramMap);
        return Result.ok();
    }


    /**
     * 转换Map的方法
     * @param request
     * @return
     */
    private Map<String, Object> getStringObjectMap(HttpServletRequest request) {
        //获取请求过来的参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        //类型转换
        return HttpRequestHelper.switchMap(parameterMap);
    }


}
